import axios, { type RawAxiosRequestHeaders } from "axios";
import { type Result } from "../models/general"


  export async function loginMethod(
      url: string,
      data: any | null
):Promise<Result> {
          const res = await axios.post(
           url,
           
               data

              
          );
            
          

          if (res.status != 200) {
            console.log("Error: " + res.status);
            return {data: null, code: res.status, msg: res.statusText};
          }
           // 存储认证信息
           if(res.data.code == 0){
               return res.data as Result;
           }
    localStorage.setItem('user_id', res.data.data.id);
    localStorage.setItem('auth_token', res.data.data.token);
   localStorage.setItem('user_name', res.data.data.name);
   localStorage.setItem('user_avatar', res.data.data.avatar);
   localStorage.setItem('user_account', res.data.data.account);

    return res.data as Result;



}


export async function generalRequest(
    url: string,
    data: any | null
  ): Promise<Result> {
    const res = await axios.post(
     url,
      {
        data: data,
      },
      {
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + localStorage.getItem('auth_token')
        },
      }
    );
    if (res.status != 200) {
      console.log("Error: " + res.status);
      return {data: null, code: res.status, msg: res.statusText};
    
    }
    return res.data as Result;
  }

  export async function registerRequest(
    url: string,
    data: any | null
  ): Promise<Result> {
    const res = await axios.post(
      url,
      
        data,
        {
        headers: {
          'Content-Type': 'application/json' // 对于包含文件的表单
        }
      }
      
      
      
    );
    if (res.status != 200) {
      console.log("Error: " + res.status);
      return {data: null, code: res.status, msg: res.statusText};
    }
    return res.data as Result;
  }